// 获取对象
var oProduct = document.querySelector(".product");
// 获取参数对象
function getUrl(url) {
    var urlArr = url.split("?")[1];
    console.log(urlArr);
    urlArr = urlArr.split("&");
    console.log(urlArr);

    var obj = {};
    urlArr.forEach(item => {
        console.log(item);
        var itemArr = item.split("=");
        console.log(itemArr);

        obj[itemArr[0]] = itemArr[1];
    });
    return obj;
}

// 主页商品详情页
(function () {
    var urlString = decodeURI(location.href);
    console.log(urlString);

    var paraObj = getUrl(urlString);
    let xhr = new XMLHttpRequest();
    xhr.open("get", "http://chst.vip:1234/api/getmoneyctrlproduct?productid=" + paraObj.productid, true);
    xhr.send();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            let res = JSON.parse(xhr.responseText);
            var result = res.result;
            console.log(result[0].productid);

            var regExp = /(?<=imgurl=)[^'"]*/;
            var img = regExp.exec(result[0].productImg2)[0];
            var ele = "";
            ele += `
         
            <div class="title">
                <p>${result[0].productName}</p>
            </div>
            
             <div class="text">
                <p>${result[0].productFrom}${result[0].productTime}</p>
                <p>${result[0].productTips}</p>
            </div> 
            
          <div class="content1">
          <img src="${img}" alt="">
                <p>${result[0].productInfo}
                ${result[0].productInfo1}
                ${result[0].productInfo2}</p>
            </div> 


             <div class="photo">
             <img src="${img}" alt="">
        </div> 
            <div class="city">
        </div>
    `
            oProduct.innerHTML = ele;

            var city = document.querySelector(".city");
            console.log(city);
            console.log(result[0].productCity);
            city.innerHTML = result[0].productCity
        }
    }
})();

// 评论
(function () {
    var urlString = decodeURI(location.href);
    var commentbox = document.querySelector("#comment");
    console.log(urlString);
    var paraObj = getUrl(urlString);
    var xhr = new XMLHttpRequest();

    xhr.open("get", "http://chst.vip:1234/api/getmoneyctrlproduct?productid=" + paraObj.productid, true);
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            var res1 = JSON.parse(xhr.responseText);
            var rersult = res1.result;
            commentbox.innerHTML = result[0].productComment;
        }
    }
})();